<template>
    <el-card>
         <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="键（Key）" prop="key">
                <el-input v-model="queryParams.key" placeholder="键（Key）" size="small"></el-input>
            </el-form-item>
            <el-form-item label="值（Value）" prop="value">
                <el-input v-model="queryParams.value" placeholder="值（Value）" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="键（Key）" prop="key"></el-table-column>
            <el-table-column label="值（Value）" prop="value"></el-table-column>
            <el-table-column label="数据类型" prop="type"></el-table-column>
            <el-table-column label="创建时间" prop="createTime"></el-table-column>
            <el-table-column label="更新时间" prop="updateTime"></el-table-column>
            <el-table-column label="操作" width="300" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row.id)">删除</el-button>
                    <el-button size="mini" type="text" icon="el-icon-bank-card" @click="editHistory(scope.row.id)">更改历史</el-button>
                </template>
            </el-table-column>
        </el-table>
         <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
         </el-pagination>
          <el-dialog :title="title" :visible.sync="formVisible" @close="resetForm">
            <el-form :model="form" :rules="rules" ref="form" :disabled="isView">
                <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="key" label="键(Key)：" :label-width="formLabelWidth" required>
                          <el-input v-model="form.key" autocomplete="off" size="mini" placeholder="请输入名称"></el-input>
                      </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="type" label="数据类型：" :label-width="formLabelWidth" required>
                          <el-select v-model="form.type" placeholder="请选择数据类型" size="small" style="width: 100%">
                                <el-option :key="1" label="布尔型" :value="1"></el-option>
                                <el-option :key="2" label="字符串型" :value="2"></el-option>
                                <el-option :key="3" label="数字型" :value="3"></el-option>
                                <el-option :key="4" label="JSON" :value="4"></el-option>
                          </el-select>
                      </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="value" label="键(Key)：" :label-width="formLabelWidth" required>
                          <el-input v-model="form.value" rows="6" type="textarea" autocomplete="off" size="mini" placeholder="请输入值(Value)"></el-input>
                      </el-form-item>
                  </el-col>
                </el-row>
            </el-form>
             <div slot="footer" class="dialog-footer" v-if="!isView">
                    <el-button type="primary" @click="handleSubmit('form')" size="small">保存</el-button>
                    <el-button @click="formVisible = false" size="small">取消</el-button>
               </div>
           </el-dialog>
    </el-card>
</template>

<script>
import { getDictList,saveDictForm,deleteResource } from '@/api/getway/dict.api.js'
    export default {
        data() {
            return {
                  tableData: [],
                  queryParams: {
                      current: 1,
                      size: 10
                  },
                  total: 0,
                  form: {},
                  formVisible: false,
                  title: '新增公共资源',
                  formLabelWidth: "120px",
                   isView: false,
                   rules: {
                     key: [
                        { required: true, message: '请输入键(Key)', trigger: 'blur' },
                     ],
                      type: [
                        { required: true, message: '请选择数据类型', trigger: 'blur' },
                     ],
                      value: [
                        { required: true, message: '请输入值(Value)', trigger: 'blur' },
                     ],
                   }
            }
        },
        mounted() {
            this.getList();
        },
        methods: {
           async getList() {
                const result = await getDictList(this.queryParams);
                if(result.code === 200) {
                    this.tableData = result.data.records;
                    this.total = result.data.total
                }
            },
             // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.formVisible = true;
            },
             resetForm() {
                this.$refs['form'].resetFields();
            },
            handleView(row) {
                this.isView = true;
                this.form = row;
                this.formVisible = true;
                this.title = '查看公共资源';
            },
            handleEdit(row) {
                this.form = row;
                this.isView = false;
                this.formVisible = true;
                this.title = '编辑公共资源'
            },
            editHistory(id) {
                this.$router.push(
                    {path:'/gateway/dictHistory',query: {id}}
                )
            },
            handleDel(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await deleteResource(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getDictData();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            handleSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                if (valid) {
                    saveDictForm(this.form).then(res=>{
                        if(res.code === 200) {
                                this.$message({
                                    message: '保存成功',
                                    type: 'success'
                                });
                            this.formVisible = false;
                            this.getDictData();
                        }
                    })
                } else {
                    return false;
                }
              });
           }
        },
    }
</script>

<style lang="scss" scoped>
.pagination-container {
    padding: 10px;
    text-align: right;
}
</style>